package xyz.jangle.finallytest;

public class TestReturn {
	
	public String test() {
		String a = "1";
		try {
			return a;	// 返回“1”
		} finally {
			System.out.println("finally");
			a="2";
		}
	}
	
	public A test2() {
		A a = new A();
		a.varA="1";
		a.varB="2";
		try {
			return a;
		} finally {
			a.setVarA("11");
			
//			A a2 = new A();
//			a2.varA="3";
//			a2.varB="4";
//			a = a2;
		}
		
	}
	
	public int test3() {
		int i = 5;
		try {
			return i;	// 返回5
		} finally {
			i=6;
		}
	}
	
	
	
	
	
	public static void main(String[] args) {
		TestReturn testReturn = new TestReturn();
		// 基本数据类型返回结果会被暂存，finally操作不会影响返回结果
		String test = testReturn.test();
		System.out.println(test);
		
		// 引用类型的对象内容被修改后，finally操作会影响返回结果（因为暂存的是对象的地址）
		A test2 = testReturn.test2();
		System.out.println(test2.getVarA());
		
		int test3 = testReturn.test3();
		System.out.println(test3);
		
	}
	
	
	
	class A {
		private String varA,varB;

		public String getVarA() {
			return varA;
		}

		public void setVarA(String varA) {
			this.varA = varA;
		}

		public String getVarB() {
			return varB;
		}

		public void setVarB(String varB) {
			this.varB = varB;
		}
		
	}

}
